//闯关模式匹配
var matchSuper = require("./matchSuper.js")
module.exports = function(manager) {
	matchSuper.call(this)
	this.manager = manager
	this.roomList = {}
	//匹配成功
	this.matchSuccess = function(players) {
		// console.log("matchSuccess")
		// console.log(players)
		var level = 0
		for(var i = 0;i < players.length;i++){
			if(players[i].level && players[i].level > level){
				level = parseInt(players[i].level)
			}
		}
		this.manager.matchSuccess("hunanRunQuicklyCheckpoint","hunanRunQuicklyCheckpoint",players,{level : level + 1})
	}
	//创建房间完成
	this.matchRoomBegin = function(roomId,players){
		this.roomList[roomId] = players
	}
	//加入匹配
	this.joinMatching = function(player,info) {
		var curTime = new Date()
		var curHours = curTime.getHours()
		var curMinutes = curTime.getMinutes()
		if(curHours > 23 || (curHours == 23 && curMinutes >= 30)){
			return "timeOver"
		}
		if(info){
			player.level = info.winCount
		}
		this.matchList[player.uid] = player
		return true
	}
	//运行匹配
	this.runMatch = function() {
		// console.log("runMatch : "+this.matchType)
		var tmpList = {}
		for(var i in this.matchList){
			var mId = this.matchList[i].level
			if(!tmpList[mId]){
				tmpList[mId] = []
			}
			tmpList[mId].push(this.matchList[i])
		}
		// console.log("tmpList",tmpList)
		for(var mId in tmpList){
			if(tmpList[mId].length < this.expectCount){
				if(tmpList[mId].length > 0){
					//有概率加入机器人
					var rand = Math.random()
					console.log(rand)
					if(rand > 0.4){
						this.joinRobot(mId)
					}
				}
			}
			if(tmpList[mId].length >= this.expectCount){
				//打乱顺序
			    var len = tmpList[mId].length;
			    for (var i = 0; i < len - 1; i++) {
			        var index = parseInt(Math.random() * (len - i));
			        var temp = tmpList[mId][index];
			        tmpList[mId][index] = tmpList[mId][len - i - 1];
			        tmpList[mId][len - i - 1] = temp;
			    }
				while(tmpList[mId].length >= this.expectCount){
					var tmpPlayers = tmpList[mId].splice(0,this.expectCount)
					for(var i = 0;i <  tmpPlayers.length;i++){
						delete this.matchList[tmpPlayers[i].uid]
					}
					this.matchSuccess(tmpPlayers)
				}
			}
		}
	}
	//加入机器人
	this.joinRobot = function(mid) {
		var uid = Math.floor(this.curRobotNum + Math.random() * 10) % this.maxRobotNum
		this.curRobotNum = uid
		var player = {
			"uid" : uid,
			"nickname" : this.robotData[uid].nickname,
			"head" : this.robotData[uid].head,
			"allGame" : Math.floor(Math.random() * 100) + 200,
			"allWin" :  Math.floor(Math.random() * 50) + 70,
			"maxWin" : Math.floor(Math.random() * 6) + 6,
			"curWin" : Math.floor(Math.random() * 6)
		}
		if(this.data && this.data.rate){
			player.maxScore = Math.pow(2,Math.floor(Math.random() * 3) + 1)  * 100 * this.data.rate
		}else{
			player.maxScore = Math.pow(2,Math.floor(Math.random() * 7) + 1)  * 1000
		}
		this.joinMatching(player,{winCount : mid})
	}
	//匹配房间结束
	this.matchRoomOver = function(roomId,info){
		console.log("匹配房间结束")
		console.log("roomId : "+roomId)
		console.log(info)
		if(this.roomList[roomId]){
			var players = this.roomList[roomId]
			if(info){
				for(var i = 0;i < players.length;i++){
					if(players[i].uid > 10000 ){
						if(players[i].uid == info.win){
							this.manager.remote.app.rpc.db.hunanRunQuickly.checkpointWin(null,players[i].uid,info.score,function(){})
						}else{
							this.manager.remote.app.rpc.db.hunanRunQuickly.checkpointLose(null,players[i].uid,function(){})
						}
					}
				}
			}
			delete this.roomList[roomId]
		}
	}
}